import fs from 'fs';
import jwt from 'jsonwebtoken';
import { Auth } from '../types';
const publicKey = fs.readFileSync(`${__dirname}/../../public.key`);
const privateKey = fs.readFileSync(`${__dirname}/../../private.key`);

export function tokenEncode (userInfo: Auth): string {
  const token = jwt.sign(userInfo, privateKey, {
    algorithm: 'RS256'
  });
  return token;
}

export function tokenDecode (token: string): Auth {
  const userInfo = jwt.verify(token, publicKey, {
    algorithms: ['RS256']
  });
  return <Auth> userInfo;
}

export default {
  encode: tokenEncode,
  decode: tokenDecode
}
